<script setup lang="ts">
import { useRouter } from "vue-router";
import { onMounted, ref } from "@vue/runtime-core"
import axios from "axios";

const router = useRouter()


interface LeftListTtpe {
    maitKey: string
    title: string
}

interface RightTopListType {
    image: string
    title: string
}

interface SortFilterType {
    sortKey: string
    title: string
}

interface RightGoodsType {
    iid: string
    img: string
    sale: number
    title: string
    price: number
    cfav: number
}


const letfList = ref<LeftListTtpe[]>([])
const rightTopList = ref<RightTopListType[]>()
const sortFilter = ref<SortFilterType[]>([])
const rightGoods = ref<RightGoodsType[]>([])
const pid = ref<string>('3627')
const sort = ref<string>('pop')
const goodsGo = ref('')

const getLeftList = () => {
    axios
        .get(`https://apis.netstart.cn/mogujie/category.json`)
        .then((({ data }) => {
            letfList.value = data.data[117330].list.map((i: LeftListTtpe) => {
                return {
                    maitKey: i.maitKey,
                    title: i.title
                }
            })
            // console.log(data);
        }))
}

const highlight = (id: string) => {
    pid.value = id
    axios
        .get(`https://apis.netstart.cn/mogujie/category/getSub?pid=${pid.value}`)
        .then((({ data }) => {
            rightTopList.value = data.data.list.map((item: RightTopListType) => {
                return {
                    image: item.image,
                    title: item.title
                }
            })
            // console.log(data);
        }))

    axios
        .get(`https://apis.netstart.cn/mogujie/category/miniWall?fcid=${pid.value}`)
        .then((data => {
            sortFilter.value = data.data.result.sortFilter.map((item: SortFilterType) => {
                return {
                    sortKey: item.sortKey,
                    title: item.title
                }
            })
            // console.log(data);
        }))
}

const categoriesList = (id: string, sorts: string) => {
    sort.value = sorts
    axios
        .get(`https://apis.netstart.cn/mogujie/category/miniWall?fcid=${pid.value}&page=2&sort=${sort.value}`)
        .then((data => {
            rightGoods.value = data.data.result.wall?.docs.map((item: RightGoodsType) => {
                return {
                    iid: item.iid,
                    img: item.img,
                    sale: item.sale,
                    title: item.title,
                    price: item.price,
                    cfav: item.cfav
                }
            })
            // console.log(data);
        }))
}

const goodsGoFn = (item: string) => {
    goodsGo.value = item
    router.push({ path: '/goods', query: { frontword: goodsGo.value } })
}


onMounted(() => {
    getLeftList()
    highlight(pid.value)
    categoriesList(pid.value, sort.value)
})

</script>

<template>

    <div>
        <div class="top-bar">
            <div class="top-left">
                <van-icon name="arrow-left" @click="$router.go(-1)" />
            </div>
            <div class="input-bar">
                <span class="search">
                    <img src="@/assets/img/search.png">
                </span>
                <input type="search" placeholder="ins超火短t" @click="$router.push({ path: '/search' })">
            </div>
            <div class="top-right"></div>
        </div>

        <div class="left-box">
            <div class="left" v-for="(l, i) in letfList" :key="i" :class="{ active: pid == l.maitKey }"
                @click="highlight(l.maitKey)">{{ l.title }}
            </div>
        </div>

        <div class="right-bar">
            <div class="right-top-box">
                <div class="right-top" v-for="(t, i) in rightTopList" :key="i" @click="goodsGoFn(t.title)">
                    <div class="top-img">
                        <van-image lazy-load :src="t.image" />
                    </div>
                    <div class="top-title">{{ t.title }}</div>
                </div>
            </div>
            <div class="right-box">
                <div class="right-bar" v-for="(b, i) in sortFilter" :key="i">
                    <div class="bar" :class="{ active: sort == b.sortKey }" @click="categoriesList(pid, b.sortKey)">{{
                            b.title
                    }}</div>
                </div>
            </div>
            <div class="list-box">
                <div class="goods-list">
                    <div class="goods" v-for="(r, i) in rightGoods" :key="i"
                        @click="$router.push({ path: '/merchandise', query: { itemId: r.iid } })">
                        <div class="goods-img">
                            <van-image lazy-load :src="r.img" />
                            <!-- <img :src="r.img"> -->
                            <span class="sale">已售{{ r.sale }}件</span>
                        </div>
                        <div class="goods-title">{{ r.title }}</div>
                        <div class="goods-price">
                            <div class="prices">
                                ¥{{ r.price }}
                            </div>
                            <div class="shouc">
                                <span>{{ r.cfav }}</span><img src="@/assets/img/xingxing2.png" alt="">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<style lang="scss" scoped>
:deep(.van-image) {
    width: 100%;
    height: 100%;
}

.top-bar {
    z-index: 1;
    display: flex;
    justify-content: space-around;
    align-items: center;
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 45px;
    background-color: #fff;

    .input-bar {
        position: relative;
        display: flex;

        .search {
            display: block;
            position: absolute;
            width: 15px;
            height: 15px;
            top: 7px;
            left: 9px;

            img {
                width: 100%;
                height: 100%;
            }
        }

        input {
            width: 256px;
            height: 30px;
            padding-left: 30px;
            border-radius: 5px;
        }


    }

    .top-right {
        width: 50px;
        height: 30px;
        background: url(@/assets/img/gouwuche2.png) no-repeat center;
        background-size: 25px 20px;
    }
}

.left-box {
    z-index: 1;
    position: fixed;
    top: 45px;
    left: 0;
    height: 100vh;
    overflow-y: auto;

    .left {
        width: 85px;
        height: 45px;
        line-height: 45px;
        text-align: center;
        color: #666666;
        background-color: #f6f6f6;
    }

    .active {
        color: #f46;
        background-color: #fff;
    }

    &::-webkit-scrollbar {
        display: none;
        /* Chrome Safari */
    }
}

.right-bar {

    .right-top-box {
        display: flex;
        flex-wrap: wrap;
        padding-top: 55px;
        padding-left: 85px;
        background: white;
        margin-bottom: 10px;
        border-bottom-left-radius: 7px;
        border-bottom-right-radius: 7px;

        .right-top {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: space-around;
            flex: 0 0 33%;
            width: 75px;
            height: 71px;
            margin-top: 10px;

            .top-title {
                font-size: 12px;
                color: #666;
            }

            .top-img {
                width: 50px;
                height: 50px;

                img {
                    width: 100%;
                    height: 100%;
                }
            }
        }
    }
}

.right-box {
    display: flex;
    flex-direction: row;
    justify-content: space-around;
    margin-top: 10px;
    padding-left: 85px;
    background-color: #fff;
    width: 100vw;
    position: sticky;
    top: 45px;
    height: 45px;
    line-height: 45px;

    .active {
        color: #f46;
    }

    .right-bar {
        width: 25vw;
        text-align: center;
        /* border-bottom: 1px solid #ccc; */
        border-top: 1px solid #ccc;
    }
}

.list-box {
    padding-left: 85px;
    margin-top: 20px;

    .goods-list {
        display: flex;
        flex-direction: row;
        align-items: center;
        flex-wrap: wrap;
        justify-content: space-evenly;

        .goods {
            flex: 0 0 45%;

            .goods-img {
                z-index: -1;
                position: relative;
                width: auto;
                height: 179px;

                img {
                    width: 100%;
                    height: 100%;
                }

                .sale {
                    position: absolute;
                    bottom: 5px;
                    left: 3px;
                    min-width: 40%;
                    max-width: 70%;
                    padding: 4px 6px;
                    background-image: url(@/assets/img/img_bg.png);
                    background-size: 100%;
                    color: #fff;
                    font-size: 12px;
                    text-align: left;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    white-space: nowrap;
                }
            }
        }

        .goods-title {
            width: 130px;
            height: 17px;
            overflow: hidden;
            text-overflow: ellipsis;
            font-size: 12px;
            white-space: nowrap;
            margin: 5px 0;
        }

        .goods-price {
            display: flex;
            justify-content: space-between;
            margin-bottom: 10px;

            .prices {
                color: #FF5577;
                font-weight: bold;
            }

            .shouc {
                display: flex;
                align-items: center;
                font-size: 12px;
                color: #666;

                span {
                    margin-right: 3px;
                }

                img {
                    width: 12px;
                    height: 12px;
                }
            }
        }
    }


}
</style>